//
//  PushtaskText.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/7.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "PushtaskText.h"

@interface PushtaskText()<UITextViewDelegate>

@property(strong,nonatomic) UILabel *placeholderLabel;

@end

@implementation PushtaskText

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5.0;
        self.layer.borderColor = [[UIColor blackColor] CGColor];
        self.layer.borderWidth = 1.0;
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self makeTextViewMainUI];
        
        
    }
    return self;
}

- (void)makeTextViewMainUI
{
    _textView = [UITextView new];
    _textView.delegate = self;
    _textView.userInteractionEnabled = NO;
    _textView.font = [UIFont systemFontOfSize:15.0f];
    [self addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
    }];
    
    _placeholderLabel = [UILabel new];
    _placeholderLabel.backgroundColor = [UIColor clearColor];
    _placeholderLabel.textColor = RGB(220, 220, 220);
    _placeholderLabel.font = [UIFont systemFontOfSize:15.0f];
    [_textView addSubview:_placeholderLabel];
    
    UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc]init];
    [singlTap addTarget:self action:@selector(handlePanFrom:)];
    [self addGestureRecognizer:singlTap];
}

- (void)handlePanFrom:(UITapGestureRecognizer *)tap
{
    if ([self.delegate respondsToSelector:@selector(clickTextView:text:)]) {
        [self.delegate clickTextView:self text:_textView.text];
    }
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    _placeholderLabel.text = placeholder;
    [_placeholderLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(7);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-6);
        make.height.mas_equalTo([UIView autoFitWithText:placeholder withFont:[UIFont systemFontOfSize:15.0f]].height);
    }];
}

- (void)setText:(NSString *)text
{
    _textView.text = text;
    if (text.length == 0) {
        _placeholderLabel.text = _placeholder;
    }else{
        _placeholderLabel.text = @"";
    }
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _placeholderLabel.text = _placeholder;
    }else{
        _placeholderLabel.text = @"";
    }
}



@end
